﻿#include "WidgetTitle.h"
#include <QMouseEvent>
#include <QStyle>
#include <QLabel>
#include <QHBoxLayout>
#include <QApplication>
#include <QFileDialog>
#include <QTime>
#include <QDateTime>
#include "GlobalUiStyle.h"
WidgetTitle::WidgetTitle(QWidget *parent) : Widget(parent),m_leftButtonPressed(false),m_start(0,0),m_end(0,0)
{
    isDouble = true;
    m_minimizeButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_minimizeButton,tr(""),TITLE_BUTTON_SIZE*3 , 0 , TITLE_BUTTON_SIZE , TITLE_BUTTON_SIZE , styleLButtonTitleMin);
    m_minimizeButton->setMouseTracking(true);
//    m_minimizeButton->move(740,0);
//    m_minimizeButton->resize(40,40);
//    m_minimizeButton->setDefaultSize(40,40);
//    m_minimizeButton->setDefaultCoordinate(740,0);

    m_maximizeButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_maximizeButton,tr(""),TITLE_BUTTON_SIZE*2 , 0 , TITLE_BUTTON_SIZE , TITLE_BUTTON_SIZE , styleLButtonTitleMax);
    m_maximizeButton->setMouseTracking(true);
    m_maximizeButton->setEnabled(true);

    m_closeButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_closeButton,tr(""),TITLE_BUTTON_SIZE , 0 , TITLE_BUTTON_SIZE , TITLE_BUTTON_SIZE , styleLButtonTitleClose);
    m_closeButton->setMouseTracking(true);

    m_optButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_optButton,tr("选项"),TITLE_BUTTON_SIZE * 6 , 8 , TITLE_BUTTON_SIZE * 2 , 25 , stylePushButtonFuncMenu);
    m_optButton->setMouseTracking(true);

    connect(m_minimizeButton, SIGNAL(clicked(bool)), this, SLOT(on_titleButtonClick()));
    connect(m_maximizeButton, SIGNAL(clicked(bool)), this, SLOT(on_titleButtonClick()));
    connect(m_closeButton, SIGNAL(clicked(bool)), this, SLOT(on_titleButtonClick()));

    /*选项按钮功能菜单*/
    m_menuOpt = new QMenu();

    m_menuOpt->setStyleSheet(stylePushButtonFuncMenu);
    m_action_1 = new QAction(m_menuOpt);
    m_action_1->setText(tr("    软件版本号    "));

    m_action_2 = new QAction(m_menuOpt);
    m_action_2->setText(tr("    软件升级      "));

    m_action_3 = new QAction(m_menuOpt);
    m_action_3->setText(tr("    切换语言:EN      "));




    connect(m_action_1,&QAction::triggered,this,&WidgetTitle::on_action_1);
    connect(m_action_2,&QAction::triggered,this,&WidgetTitle::on_action_2);
    connect(m_action_3,&QAction::triggered,this,&WidgetTitle::on_action_3);


    m_menuOpt->addAction(m_action_1);
    m_menuOpt->addAction(m_action_2);
    m_menuOpt->addAction(m_action_3);

    m_optButton->setMenu(m_menuOpt);



    /*子功能栏使能按钮*/
//    SwitchButtonInfo info;


//    info.isChecked = false;
//    info.trueValue = 1;
//    info.falseValue = 0;

//    m_labelTime = new Label(this);
//    Label::initLabel(m_labelTime,"",1314,66,400,25,styleLabelText);

//    m_timer = new QTimer(this);
//    m_timer->setInterval(1000);
//    connect(m_timer,&QTimer::timeout,this,&WidgetTitle::on_systemTimeOut);
//    this->on_systemTimeOut();
//    m_timer->start();

    this->addResizeBaseObj(m_minimizeButton);
    this->addResizeBaseObj(m_maximizeButton);
    this->addResizeBaseObj(m_closeButton);
    this->addResizeBaseObj(m_optButton);


    m_optButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);
    m_minimizeButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);
    m_maximizeButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);
    m_closeButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);
//    m_labelTime->hide();

}

void WidgetTitle::mouseDoubleClickEvent(QMouseEvent *event)
{
    emit m_maximizeButton->click();
}

void WidgetTitle::mousePressEvent(QMouseEvent *event)
{
    Widget::mousePressEvent(event);
//    qDebug()<<"导航栏按下了"<<endl;
    if (event->button() == Qt::LeftButton)
    {
        m_leftButtonPressed = true;

        m_start = event->globalPos();
    }
}

void WidgetTitle::mouseMoveEvent(QMouseEvent *event)
{
    Widget::mouseMoveEvent(event);
//    qDebug()<<"到导航栏了：x:"<<event->globalX()<<" y:"<<event->globalY()<<endl;
    if(m_leftButtonPressed)
    {
        parentWidget()->move(parentWidget()->geometry().topLeft() +
                             event->globalPos() - m_start);

        m_start = event->globalPos();
        emit evt_moveParentWidget();
    }
}

void WidgetTitle::mouseReleaseEvent(QMouseEvent *event)
{
    Widget::mouseReleaseEvent(event);
    if (event->button() == Qt::LeftButton)
    {

        m_leftButtonPressed = false;
    }
}

bool WidgetTitle::eventFilter(QObject *obj, QEvent *event)
{
    switch(event->type())
    {

    case QEvent::WindowStateChange:
    case QEvent::Resize:
        updateMaximize();
        return true;
    }
    return QWidget::eventFilter(obj, event);
}


void WidgetTitle::updateMaximize()
{
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        bool bMaximize = pWindow->isMaximized();
        if (bMaximize)
        {
            m_maximizeButton->setToolTip("Restore");
            m_maximizeButton->setProperty("maximizeProperty", "restore");
        }
        else
        {
            m_maximizeButton->setProperty("maximizeProperty", "maximize");
            m_maximizeButton->setToolTip("Maximize");
        }

        m_maximizeButton->setStyle(QApplication::style());
    }
}

void WidgetTitle::on_titleButtonClick()
{
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        if (pButton == m_minimizeButton)
        {
            pWindow->showMinimized();
        }
        else if (pButton == m_maximizeButton)
        {
            pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();
        }
        else if (pButton == m_closeButton)
        {
            pWindow->close();
        }
    }
}





void WidgetTitle::on_systemTimeOut()
{
    QDateTime CurrentTime = QDateTime::currentDateTime();
    QString Timestr=CurrentTime.toString(" yyyy年 MM月 dd日 ddd  a hh:mm:ss "); //设置显示的格式   currentTimeLabel->setText(Timestr);

    m_labelTime->setText(Timestr);
}


void WidgetTitle::on_action_1()
{
    DialogPop *dialog = new DialogPop(this);
    dialog->hideCancelButton();
//    dialog->setText(SOFTWARE_VERSION);
    dialog->setText("SOFTWARE_VERSION");
    int i = dialog->exec();

    if(i == QDialog::Accepted)
    {
        qDebug()<<"确定";
    }
    if(i == QDialog::Rejected)
    {
        qDebug()<<"取消";
    }
}

void WidgetTitle::on_action_2()
{
    qDebug()<<"software update";
}
#include <QProcess>
void WidgetTitle::on_action_3()
{
      qDebug()<<"qrwidget run";
      emit evt_showWidgetQRcode();
}

void WidgetTitle::on_action_4()
{
    qDebug()<<"change language!"<<endl;
}

WidgetTitle_dialog::WidgetTitle_dialog(QWidget *parent) : Widget(parent),m_leftButtonPressed(false),m_start(0,0),m_end(0,0)
{

    m_minimizeButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_minimizeButton,tr(""),TITLE_BUTTON_SIZE*3 , 0 , TITLE_BUTTON_SIZE , TITLE_BUTTON_SIZE , styleLButtonTitleMin);


    m_maximizeButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_maximizeButton,tr(""),TITLE_BUTTON_SIZE*2 , 0 , TITLE_BUTTON_SIZE , TITLE_BUTTON_SIZE , styleLButtonTitleMax);

    m_closeButton = new PushButtonTitle(this);
    PushButtonTitle::initButton(m_closeButton,tr(""),TITLE_BUTTON_SIZE , 0 , TITLE_BUTTON_SIZE , TITLE_BUTTON_SIZE , styleLButtonTitleClose);

    connect(m_minimizeButton, SIGNAL(clicked(bool)), this, SLOT(on_titleButtonClick()));
    connect(m_maximizeButton, SIGNAL(clicked(bool)), this, SLOT(on_titleButtonClick()));
    connect(m_closeButton, SIGNAL(clicked(bool)), this, SLOT(on_titleButtonClick()));




    /*子功能栏使能按钮*/
    SwitchButtonInfo info;


    info.isChecked = false;
    info.trueValue = 1;
    info.falseValue = 0;
    m_pushbuttonAdmin = new PushButtonSwitch(this);
    PushButtonSwitch::initButton(m_pushbuttonAdmin,"  管理员",1754,54,129,40,stylePushButtonAdmin_on,stylePushButtonAdmin_off,info);
    connect(m_pushbuttonAdmin,&PushButtonSwitch::evt_valueChanged,this,&WidgetTitle_dialog::on_pushbuttonAdminClick);



    this->addResizeBaseObj(m_minimizeButton);
    this->addResizeBaseObj(m_maximizeButton);
    this->addResizeBaseObj(m_closeButton);


    m_minimizeButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);
    m_maximizeButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);
    m_closeButton->setAutoResize(UI_RESIZE_POS_FIX_Y_REVFIX_X_FIX_SIZE_FIX);





}

void WidgetTitle_dialog::hideAllChildren(bool flag)
{
    m_minimizeButton->setHidden(flag);
    m_maximizeButton->setHidden(flag);
    m_closeButton->setHidden(flag);
}


//void WidgetTitle_dialog::resetLayout()
//{
//m_closeButton->move(this->width() - m_closeButton->width() * 1 , 0);
//m_maximizeButton->move(this->width() - m_closeButton->width() * 2 , 0);
//m_minimizeButton->move(this->width() - m_minimizeButton->width() * 3 , 0);
//}

PushButtonSwitch *WidgetTitle_dialog::getAdminMenuButton()
{
    return m_pushbuttonAdmin;
}

void WidgetTitle_dialog::mouseDoubleClickEvent(QMouseEvent *event)
{
    m_maximizeButton->click();
}

void WidgetTitle_dialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug()<<" WidgetTitle_dialog::mousePressEvent";
        m_leftButtonPressed = true;

        m_start = event->globalPos();
    }

}

void WidgetTitle_dialog::mouseMoveEvent(QMouseEvent *event)
{
    if(m_leftButtonPressed)
    {
        parentWidget()->move(parentWidget()->geometry().topLeft() +
                             event->globalPos() - m_start);

        m_start = event->globalPos();
    }
}

void WidgetTitle_dialog::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {

        m_leftButtonPressed = false;
    }
}

bool WidgetTitle_dialog::eventFilter(QObject *obj, QEvent *event)
{
    switch(event->type())
    {

    case QEvent::WindowStateChange:
    case QEvent::Resize:
        updateMaximize();
        return true;
    }
    return QWidget::eventFilter(obj, event);
}


void WidgetTitle_dialog::updateMaximize()
{
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        bool bMaximize = pWindow->isMaximized();
        if (bMaximize)
        {
            m_maximizeButton->setToolTip("Restore");
            m_maximizeButton->setProperty("maximizeProperty", "restore");
        }
        else
        {
            m_maximizeButton->setProperty("maximizeProperty", "maximize");
            m_maximizeButton->setToolTip("Maximize");
        }

        m_maximizeButton->setStyle(QApplication::style());
    }
}

void WidgetTitle_dialog::on_titleButtonClick()
{
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        if (pButton == m_minimizeButton)
        {
            pWindow->showMinimized();
        }
        else if (pButton == m_maximizeButton)
        {
            pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();
        }
        else if (pButton == m_closeButton)
        {
            pWindow->close();
        }
    }
}




void WidgetTitle_dialog::on_pushbuttonAdminClick()
{
    int temp = m_pushbuttonAdmin->getValue();
    qDebug()<<"on_pushbuttonAdminClick"<<temp;
    emit evt_pushbuttonAdminClick(temp);
}


